//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
const dingyue = require("../../common/dingyue.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: "",
    type: '0',
    top: [{
      'title': '待付款',
      'active': true,
      'type': '0'
    },
    {
      'title': '已支付',
      'active': false,
      'type': '1'
    },
    {
      'title': '已完成',
      'active': false,
      'type': '2'
    }
    ],
    // 页数
    pageIndex: 1,
    // 所有商家列表
    orderList: [],
    orderList1: [],
    orderList2: [],
    state: '',
    // 卖家
    isbusinessOrder: 1,
    // 订单id
    orderid: 0,
    // 输入邮费-弹框
    youfei: '',
    hiddensubmit: true,
    nocancel: false,
    // 退款
    tuikuan: '',
    hiddensubmit1: true,
    //修改金额
    hiddensubmit2: true,
    // 确认收货
    hiddensubmit3: true,
    disabled: {
      type: "",
      value: "",
    },
    zongjia: "",//修改总价
    BuyGoodsName: "",//商品名称
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.type);
    // console.log("0000000");
    if (options.type) {
      this.setData({
        type: options.type,
        'top[1].active': true,
        'top[0].active': false
      })
    }
    this.setData({
      uid: wx.getStorageSync('uid'),
      // orderid: options.orderid,
    })
    // console.log(this.data.orderid);
    this.GetOrderList();
  },

  // 顶部top
  topTap: function (e) {
    var top = this.data.top;
    var type = e.currentTarget.dataset.type;
    for (var i = 0, len = top.length; i < len; ++i) {
      if (top[i].type == type) {
        top[i].active = true;
      } else {
        top[i].active = false
      }
    }
    this.setData({
      top: top,
      type: type
    })
    // console.log(type);
    // console.log(top);
    this.GetOrderList1();
    wx.setStorageSync('type', e.currentTarget.dataset.type);

  },
  // 获取收银机订单信息
  GetOrderList: function () {
    var that = this;
    var uid = this.data.uid;
    var pageIndex = this.data.pageIndex;
    var state = this.data.state;
    var type = this.data.type;
    var orderList = this.data.orderList;
    var orderList1 = this.data.orderList1;
    var orderList2 = this.data.orderList2;
    var isbusinessOrder = this.data.isbusinessOrder;
    // console.log(isbusinessOrder)
    if (isbusinessOrder == '1') {
      if (type == '0') {
        console.log(baseJs.api_Order + "GetOrderList/" + uid + "?" + "PageIndex=" + pageIndex + "&PageSize=" + '10' + "&OrderType=" + '1' + "&OrderState=" + type + "&IsBusinessOrder=" + '1')
        wx.request({
          url: baseJs.api_Order + "GetOrderList/" + uid + "?" + "PageIndex=" + pageIndex + "&PageSize=" + '10' + "&OrderType=" + '1' + "&OrderState=" + type + "&IsBusinessOrder=" + '1',

          success: function (res) {
            // console.log(res.data)
            var json = res.data.ResultData;
            // console.log(json);
            for (var i = 0; i < json.length; i++) {
              orderList.push(json[i])
            }
            that.setData({
              orderList: orderList,
            })
            console.log(orderList);

            if (json.length == 10) {
              that.setData({
                state: 'loadMore',
              })

            } else if (json.length == 0 && pageIndex == 1) {
              that.setData({
                state: 'noData',
              })
            } else if (json.length < 10) {
              that.setData({
                state: 'noData',
              })
            }
            wx.setStorageSync('isbusinessOrder', '1')
          },
          fail(res) {
            console.log(res)
          }

        })
      } else if (type == '1') {
        console.log(baseJs.api_Order + "GetOrderList/" + uid + "?" + "PageIndex=" + pageIndex + "&PageSize=" + '10' + "&OrderType=" + '1' + "&OrderState=" + type + "&IsBusinessOrder=" + '1')
        wx.request({
          url: baseJs.api_Order + "GetOrderList/" + uid + "?" + "PageIndex=" + pageIndex + "&PageSize=" + '10' + "&OrderType=" + '1' + "&OrderState=" + type + "&IsBusinessOrder=" + '1',
          success: function (res) {
            // console.log(res.data)
            var json1 = res.data.ResultData;
            // console.log(json1);
            for (var i = 0; i < json1.length; i++) {
              orderList1.push(json1[i])
            }
            that.setData({
              orderList1: orderList1,
            })
            console.log(orderList1);

            if (json1.length == 10) {
              that.setData({
                state: 'loadMore',
              })
            } else if (json1.length == 0 && pageIndex == 1) {
              that.setData({
                state: 'noData',
              })
            } else if (json1.length < 10) {
              that.setData({
                state: 'noData',
              })
            }
            wx.setStorageSync('isbusinessOrder', '1')
          },
          fail(res) {
            console.log(res)
          }

        })
      } else {
        console.log(baseJs.api_Order + "GetOrderList/" + uid + "?" + "PageIndex=" + pageIndex + "&PageSize=" + '10' + "&OrderType=" + '1' + "&OrderState=" + type + "&IsBusinessOrder=" + '1')
        wx.request({
          url: baseJs.api_Order + "GetOrderList/" + uid + "?" + "PageIndex=" + pageIndex + "&PageSize=" + '10' + "&OrderType=" + '1' + "&OrderState=" + type + "&IsBusinessOrder=" + '1',
          success: function (res) {
            // console.log(res.data)
            var json2 = res.data.ResultData;
            // console.log(json2);
            for (var i = 0; i < json2.length; i++) {
              orderList2.push(json2[i])
            }
            that.setData({
              orderList2: orderList2,
            })
            console.log(orderList2);

            if (json2.length == 10) {
              that.setData({
                state: 'loadMore',
              })
            } else if (json2.length == 0 && pageIndex == 1) {
              that.setData({
                state: 'noData',
              })
            } else if (json2.length < 10) {
              that.setData({
                state: 'noData',
              })
            }
            wx.setStorageSync('isbusinessOrder', '1')
          },
          fail(res) {
            console.log(res)
          }

        })
      }
    }
  },
  // 刷新
  GetOrderList1: function () {
    var that = this;
    var uid = this.data.uid;
    var pageIndex = this.data.pageIndex;
    var state = this.data.state;
    var type = this.data.type;
    var orderList = this.data.orderList;
    var orderList1 = this.data.orderList1;
    var orderList2 = this.data.orderList2;
    var isbusinessOrder = this.data.isbusinessOrder;
    // console.log(isbusinessOrder)
    if (isbusinessOrder == '1') {
      if (type == '0') {
        // console.log(baseJs.api_Order + "GetOrderList/" + uid + "?" + "PageIndex=" + '1' + "&PageSize=" + '10' + "&OrderType=" + '1' + "&OrderState=" + type + "&IsBusinessOrder=" + '1')
        wx.request({
          url: baseJs.api_Order + "GetOrderList/" + uid + "?" + "PageIndex=" + '1' + "&PageSize=" + '10' + "&OrderType=" + '1' + "&OrderState=" + type + "&IsBusinessOrder=" + '1',
          success: function (res) {
            // console.log(res.data)
            var json = res.data.ResultData;
            // console.log(json);
            orderList = [];
            for (var i = 0; i < json.length; i++) {
              orderList.push(json[i])
            }
            that.setData({
              orderList: orderList,
            })
            console.log(orderList);

            if (json.length == 10) {
              that.setData({
                state: 'loadMore',
              })

            } else if (json.length == 0 && pageIndex == 1) {
              that.setData({
                state: 'noData',
              })
            } else if (json.length < 10) {
              that.setData({
                state: 'noData',
              })
            }
          },
          fail(res) {
            console.log(res)
          }

        })
      } else if (type == '1') {
        wx.request({
          url: baseJs.api_Order + "GetOrderList/" + uid + "?" + "PageIndex=" + '1' + "&PageSize=" + '10' + "&OrderType=" + '1' + "&OrderState=" + type + "&IsBusinessOrder=" + '1',
          success: function (res) {
            // console.log(res.data)
            var json1 = res.data.ResultData;
            // console.log(json1);
            orderList1 = [];
            for (var i = 0; i < json1.length; i++) {
              orderList1.push(json1[i])
            }
            that.setData({
              orderList1: orderList1,
            })
            console.log(orderList1);
            if (json1.length == 10) {
              that.setData({
                state: 'loadMore',
              })
            } else if (json1.length == 0 && pageIndex == 1) {
              that.setData({
                state: 'noData',
              })
            } else if (json1.length < 10) {
              that.setData({
                state: 'noData',
              })
            }
          },
          fail(res) {
            console.log(res)
          }

        })
      } else {
        wx.request({
          url: baseJs.api_Order + "GetOrderList/" + uid + "?" + "PageIndex=" + '1' + "&PageSize=" + '10' + "&OrderType=" + '1' + "&OrderState=" + type + "&IsBusinessOrder=" + '1',
          success: function (res) {
            // console.log(res.data)
            var json2 = res.data.ResultData;
            // console.log(json2);
            orderList2 = [];
            for (var i = 0; i < json2.length; i++) {
              orderList2.push(json2[i])
            }
            that.setData({
              orderList2: orderList2,
            })
            console.log(orderList2);

            if (json2.length == 10) {
              that.setData({
                state: 'loadMore',
              })
            } else if (json2.length == 0 && pageIndex == 1) {
              that.setData({
                state: 'noData',
              })
            } else if (json2.length < 10) {
              that.setData({
                state: 'noData',
              })
            }
          },
          fail(res) {
            console.log(res)
          }

        })
      }
    }
  },
  /**
    * 页面上拉触底事件的处理函数
    */
  onReachBottom: function () {
    this.data.pageIndex++;
    this.GetOrderList();
  },
  // 查看详情跳转-订单详情
  Orderdetails: function (event) {
    var id = event.currentTarget.id;
    var type = wx.getStorageSync('type');
    var isbusinessOrder = wx.getStorageSync('isbusinessOrder')
    wx.navigateTo({
      url: '/packageA/Orderdetails/Orderdetails?orderid=' + id + '&Ordertype=' + type + '&isbusinessOrder=' + isbusinessOrder,
    })
  },

  // 邮费获取
  MaInput: function (e) {
    console.log(e)
    var youfei = this.data.youfei;
    this.setData({
      youfei: e.detail.value,
    })
  },

  // 输入邮费-modal弹框
  formSubmit(e) {
    console.log(e)
    var that = this;
    // this.data.orderid = e.currentTarget.dataset.id;
    var hiddensubmit = this.data.hiddensubmit;
    that.setData({
      hiddensubmit: !this.data.hiddensubmit,
      orderid: e.currentTarget.id,
      BuyGoodsName: e.currentTarget.dataset.value.BuyGoodsName
    })
    console.log(that.data.orderid);
    // wx.setStorageSync('id', e.currentTarget.dataset.id);
  },
  // 弹框输入邮费-取消
  cancel: function () {
    this.setData({
      hiddensubmit: true,
      youfei: ""
    });
  },
  // 弹框输入邮费-确定
  confirm: function (e) {
    console.log(e);
    var that = this;
    var uid = this.data.uid
    var id = e.currentTarget.dataset.id;
    var orderId = this.data.orderid
    // console.log(orderId);
    // console.log(uid);
    // console.log(id);
    var youfei = this.data.youfei;
    // console.log(youfei);
    // var id = wx.getStorageSync('id');
    console.log(baseJs.api_Order + "UpdateOrderYouFei/" + uid + "/" + orderId + "?" + "YouFei=" + youfei);
    wx.request({
      url: baseJs.api_Order + "UpdateOrderYouFei/" + uid + "/" + that.data.orderid + "?" + "YouFei=" + youfei,
      success: function (res) {
        console.log(res)
        var json = res.data.ResultData;
        // console.log(json);
        if (res.data.ResultFlag == 1) {
          wx.showToast({
            title: res.data.ResultMsg,
            icon: 'none',
            duration: 2000
          });
          that.GetOrderList1();
          // 卖家设置完邮费通知买家支付邮费
          youfei = "￥" + youfei
          var goodsname = that.data.BuyGoodsName
          dingyue.setYoufei(youfei, goodsname)
        }
        if (res.data.ResultFlag == 0) {
          wx.showToast({
            title: res.data.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      },
      fail: function () {
        console.log("错误");
      }
    })
    this.setData({
      hiddensubmit: true,
      youfei: ""
    });
  },


  // 同意退款-modal弹框
  agreeRefund(e) {
    console.log(e);

  },
  // 同意退款--取消
  cancel1: function () {
    this.setData({
      hiddensubmit1: true
    });
  },
  // 同意退款--确定
  confirm1: function (e) {
    console.log(e);
    var that = this;
    var uid = this.data.uid;
    var id = e.currentTarget.dataset.value.OrderId;
    console.log(baseJs.api_Order + "AgreeOrderRefund/" + uid + "/" + id);
    wx.request({
      url: baseJs.api_Order + "AgreeOrderRefund/" + uid + "/" + id,
      success: function (res) {
        console.log(res.data)
        // var json = res.data.ResultData;
        // console.log(json);
        if (res.data.ResultFlag == 1) {
          wx.showToast({
            title: res.data.ResultMsg,
            icon: 'none',
            duration: 2000
          });
          that.GetOrderList1();
          var tuikuanMoney = "￥" + e.currentTarget.dataset.value.PayAllMa //退款金额
          var tuikuanRes = res.data.ResultMsg//退款结果
          var goodsName = e.currentTarget.dataset.value.BuyGoodsName//商品的名称
          // 同意退款给买家发送信息
          dingyue.tuikuanPass(tuikuanMoney, tuikuanRes, goodsName)
        }
        if (res.data.ResultFlag == 0) {
          wx.showToast({
            title: res.data.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }

      }
    })
    this.setData({
      hiddensubmit1: true
    });
  },
  // 自提-立即支付
  payMent: function (event) {
    var id = event.currentTarget.id;
    wx.navigateTo({
      url: '/packageA/Orderpayment/Orderpayment?orderid=' + id + "&show=0",
    })
  },
  // 延长收货--modal弹框
  // yanChang(e) {
  //   // console.log(e)
  //   var that = this;
  //   var id = e.currentTarget.dataset.id;
  //   var hiddensubmit2 = this.data.hiddensubmit2;
  //   that.setData({
  //     hiddensubmit2: !this.data.hiddensubmit2,
  //     id: id
  //   })
  // },
  // 延长收货--取消
  // cancel2: function () {
  //   this.setData({
  //     hiddensubmit2: true
  //   });
  // },
  // // 延长收货--确定
  // confirm2: function (e) {
  //   var that = this;
  //   var uid = this.data.uid;
  //   var id = e.currentTarget.dataset.id;
  //   console.log(id)
  //   console.log(baseJs.api_Order + "ExtendOrderTime/" + uid + "/" + id);
  //   wx.request({
  //     url: baseJs.api_Order + "ExtendOrderTime/" + uid + "/" + id,
  //     success: function (res) {
  //       console.log(res.data)
  //       var json = res.data.ResultData;
  //       // console.log(json);
  //       if (res.data.ResultFlag == 1) {
  //         wx.showToast({
  //           title: res.data.ResultMsg,
  //           icon: 'none',
  //           duration: 2000
  //         });
  //         // setTimeout(function () {
  //         //   wx.navigateBack({
  //         //     delta: 1
  //         //   })
  //         // }, 1000)
  //       }
  //       if (res.data.ResultFlag == 0) {
  //         wx.showToast({
  //           title: res.data.ResultMsg,
  //           icon: 'none',
  //           duration: 2000
  //         });
  //       }

  //     }
  //   })
  //   this.setData({
  //     hiddensubmit2: true
  //   });
  // },


  // 确认收货--modal弹框
  queRen(e) {
    // console.log(e)
    var that = this;
    var id = e.currentTarget.dataset.id;
    var hiddensubmit3 = this.data.hiddensubmit3;
    that.setData({
      hiddensubmit3: !this.data.hiddensubmit3,
      id: id
    })
  },
  // 确认收货--取消
  cancel3: function () {
    this.setData({
      hiddensubmit3: true,
    });
  },
  // 确认收货--确定
  confirm3: function (e) {
    var that = this;
    var uid = this.data.uid;
    var id = e.currentTarget.dataset.id;
    console.log(id)
    console.log(baseJs.api_Order + "ConfirmFinishOrder/" + uid + "/" + id);
    wx.request({
      url: baseJs.api_Order + "ConfirmFinishOrder/" + uid + "/" + id,
      success: function (res) {
        console.log(res.data)
        var json = res.data.ResultData;
        // console.log(json);
        if (res.data.ResultFlag == 1) {
          wx.showToast({
            title: res.data.ResultMsg,
            icon: 'none',
            duration: 2000
          });
          // setTimeout(function () {
          //   wx.navigateBack({
          //     delta: 1
          //   })
          // }, 1000)
          that.GetOrderList1();
        }
        if (res.data.ResultFlag == 0) {
          wx.showToast({
            title: res.data.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }

      }
    })
    this.setData({
      hiddensubmit3: true
    });
  },


  // 修改总价弹出modal
  changeMoney(e) {
    console.log(e);
    this.setData({
      hiddensubmit2: false,
      orderid: e.currentTarget.dataset.value.OrderId
    })
    // console.log(this.data.orderid);
  },
  // 获取修改的总价的值
  MaInput2(e) {
    this.setData({
      zongjia: e.detail.value
    })
  },
  // 取消修改总价
  cancel2() {
    this.setData({
      hiddensubmit2: true,
      zongjia: "",
    })
  },
  // 确定修改总计
  confirm2() {
    var uid = this.data.uid
    var OrderMoney = this.data.zongjia
    var id = this.data.orderid
    console.log(id);
    console.log(OrderMoney);
    wx.request({
      url: baseJs.api_Order + "UpdateOrderMoney/" + uid + "/" + id + "?" + "OrderMoney=" + OrderMoney,
      success: res => {
        var data = res.data
        if (data.ResultFlag == 1) {
          wx.showToast({
            title: data.ResultMsg,
            icon: "success"
          })
          this.GetOrderList1()
        } else {
          wx.showToast({
            title: data.ResultMsg,
            icon: "error"
          })
        }
      }
    })
    this.setData({
      zongjia: "",
      hiddensubmit2: true
    })
  },

  //一键上架
  loadGoods(e) {
    var OrderId = e.currentTarget.dataset.value.OrderId
    wx.request({
      url: baseJs.api_Order + "BusinessOneKeyLoadGoods/" + uid + "/" + OrderId,
      success: res => {
        var data = res.data
        if (data.ResultFlag == 1) {
          wx.showToast({
            title: data.ResultMsg,
            icon: "success"
          })
        } else {
          wx.showToast({
            title: data.ResultMsg,
            icon: "error"
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.GetOrderList1
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})